package com.example.demo.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Token;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface TokenMapper extends BaseMapper<Token> {

    @Select("<script>select id,user_id,token,create_time from token where 1=1 "+
            "<if test=\" userId !=null and userId != '' \">and user_id = #{userId} </if>"+
            "</script>")
    @Results(id = "BaseResultMap",value ={
            @Result(property = "id",column = "id",id = true),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "token",column = "token"),
            @Result(property = "createTime",column = "create_time"),
    })
    Token findByUserId(Token token);

    @Select("select nickName from `user` left join token on `user`.uid = token.user_id where token.token = #{token}")
    String findUserName(String token);

    @Delete("delete from token where token = #{token}")
    int deleteByToken(String token);

    @Select("select u.* from user u left join token t on u.uid = t.user_id where t.token = #{token}")
    User selectCurrentUser(String token);
}
